home *** CD-ROM | disk | FTP | other *** search
- /* This program contains
- * the bodies of routines that convert image forms from one to another
- * All of them work with a sigle line of image.
- *
- * And one important note!!!
- * YOU MUST reserve one extra byte in dest. line, because
- * (please, forgive me) some procedures tends to write to this byte
- *
- * Written by E.Podvoysky & Kiselev J. CZ 1994.
- */
-
- /* see image form descriptions in grfile.h */
-
- #pragma -ml
-
- #include <mem.h>
- #include "common.h"
- #include "formconv.h"
-
- void invert_order(BYTE *source,BYTE *dest,int width) {
- register int i;
- register BYTE *s,*d;
-
- s = source; d = dest + width - 1;
- for (i = width; i > 0; i--) *(d--) = *(s++);
-
- }
-
- void mono2row(BYTE *source,BYTE *dest,BYTE whitecolor,int width){
- register int i=0,j;
- register BYTE b;
- while (TRUE) {
- b = *(source++);
- for (j = 0; j < 8; j++) {
- *(dest++) = (b & 0x80) ? whitecolor : 0;
- b <<= 1;
- if (i++ >= width) return;
- }
- }
- }
-
- void monoGIF2mono(BYTE *source,BYTE *dest,int width) {
- register int j;
- for(j=0;j<width;j++)
- if(source[j] == 0) dest[j/8] <<= 1;
- else {dest[j/8] <<= 1; dest[j/8]++;}
- }
-
- void mono2GIFmono(BYTE *source,BYTE *dest,int width) {
- mono2row(source,dest,1,width);
- }
-
- void mono2mono(BYTE *source,BYTE *dest,int width) {
- memmove(dest,source,(width+7)/8);
- }
-
- void gray2byte(BYTE *source,BYTE *dest,int width) { // = byte2gray
- memmove(dest,source,width);
- }
-
- void col2562byte(BYTE *source,BYTE *dest,int width) { // = byte2col256
- memmove(dest,source,width);
- }
-
- void col16A2col16B(BYTE *source,BYTE *dest,int width) { //16col2byte
- register int i;
- register BYTE b;
- for (i = (width+1)/2; i > 0; i--) {
- b = *(source++);
- *(dest++) = b >> 4;
- *(dest++) = b & 0x0F;
- }
- }
-
- void col16plane2col16B(BYTE *source,BYTE *dest,int width){ //16col2byte
- register int j;
- for(j=0;j<width;j++)
- // 4 planes
- *(dest++) = ((source[j/8] & (0x80 >> j%8) ) >> 7-j%8) |
- (((source[j/8+(width+7)/8] & (0x80 >> j%8)) >> 7-j%8) << 1) |
- (((source[j/8+(width+7)/8*2] & (0x80 >> j%8)) >> 7-j%8) << 2) |
- (((source[j/8+(width+7)/8*3] & (0x80 >> j%8)) >> 7-j%8) << 3);
- }
-
- void col16B2col16A(BYTE *source,BYTE *dest,int width) {
- register int i;
- for(i=(width+1)/2;i>0;i--) {
- *(dest) = *(source++) << 4;
- *(dest++) |= *(source++);
- }
- }
-
- void col16B2col16plane(BYTE *source,BYTE *dest,int width){
-
- register BYTE b;
- register int i,j;
- BYTE *p0 = dest;
- BYTE *p1 = &dest[(width+7)/8];
- BYTE *p2 = &dest[(width+7)/8*2];
- BYTE *p3 = &dest[(width+7)/8*3];
-
- for(i = (width+7)/8; i > 0; i--) {
- for(j = 0; j < 8; j++) {
- b = *(source++);
- *p0 = (*p0 << 1) | (b & 1);
- *p1 = (*p1 << 1) | ((b >> 1) & 1);
- *p2 = (*p2 << 1) | ((b >> 2) & 1);
- *p3 = (*p3 << 1) | (b >> 3);
- }
- p0++; p1++; p2++; p3++;
- }
- }
-
- void mono2RGBlines(BYTE *source, BYTE *dest,int width) {
- register int j;
- BYTE *rp = dest;
- BYTE *gp = &dest[width];
- BYTE *bp = &dest[width*2];
- for(j=0;j<width;j++) {
- if((source[j/8] << 7-j%8) == 1) *(rp++) = *(gp++) = *(bp++) = 255;
- else *(rp++) = *(gp++) = *(bp++) = 0;
- }
- }
-
- void byte2RGBlines(BYTE *source, BYTE *dest,int width,BGRpalette pal) {
- register int j;
- BYTE *rp = dest;
- BYTE *gp = &dest[width];
- BYTE *bp = &dest[width*2];
- for(j=0;j<width;j++) {
- *(rp++) = pal[source[j]].r;
- *(gp++) = pal[source[j]].g;
- *(bp++) = pal[source[j]].b;
- }
- }
-
- void BGR2RGBlines(BYTE *source, BYTE *dest, int width) {
- register int i;
- register BYTE *src;
- BYTE *rp = dest;
- BYTE *gp = &dest[width];
- BYTE *bp = &dest[width*2];
- src = source;
- for (i = width; i > 0; i--) {
- *(bp++) = *(src++);
- *(gp++) = *(src++);
- *(rp++) = *(src++);
- }
- }
-
- void RGB15_2RGBlines(BYTE *source, BYTE *dest, int width) {
- int i;
- register WORD w;
- register WORD *src;
- BYTE *rp = dest;
- BYTE *gp = &dest[width];
- BYTE *bp = &dest[width*2];
- src = (WORD *)source;
- for (i = width; i > 0; i--) {
- w = *(src++);
- *(rp++) = ((w >> 10) & 0x1F) << 3;
- *(gp++) = ((w >> 5) & 0x1F) << 3;
- *(bp++) = ( w & 0x1F) << 3;
- }
- }
-
- void RGB16_2RGBlines(BYTE *source, BYTE *dest, int width) {
- int i;
- register WORD w;
- register WORD *src;
- BYTE *rp = dest;
- BYTE *gp = &dest[width];
- BYTE *bp = &dest[width*2];
-
- src = (WORD *)source;
- for (i = width; i > 0; i--) {
- w = *(src++);
- *(rp++) = ((w >> 11) & 0x1F) << 3;
- *(gp++) = ((w >> 6) & 0x3F) << 2;
- *(bp++) = (w & 0x1F) << 3;
- }
- }
-
- void BGRA32_2RGBlines(BYTE *source, BYTE *dest, int width) {
- register int i;
- register BYTE *src;
- BYTE *rp = dest;
- BYTE *gp = &dest[width];
- BYTE *bp = &dest[width*2];
-
- src = source;
- for (i = width; i > 0; i--) {
- *(bp++) = *(src++);
- *(gp++) = *(src++);
- *(rp++) = *(src++);
- src++;
- }
- }
-
- void RGBplanes2RGBlines(BYTE *source, BYTE *dest, int width) {
- memcpy(dest,source,width*3);
- }
-
- void RGBlines2BGR(BYTE *source, BYTE *dest, int width) {
- register int i;
- BYTE *rp = source;
- BYTE *gp = &source[width];
- BYTE *bp = &source[width*2];
-
- for(i=0;i<width;i++) {
- *(dest++) = *(bp++);
- *(dest++) = *(gp++);
- *(dest++) = *(rp++);
- }
- }
-
- void RGBlines2RGB15(BYTE *source, BYTE *dest, int width) {
- register int i;
- register WORD *dst = (WORD *)dest;
- BYTE *rp = source;
- BYTE *gp = &source[width];
- BYTE *bp = &source[width*2];
-
- for (i = width; i > 0; i--)
- *(dst++) = (*(rp++) << 10) | (*(gp++) << 5) | *(bp++);
- }
-
- void RGBlines2BGRA32(BYTE *source, BYTE *dest, int width) {
- register int i;
- BYTE *rp = source;
- BYTE *gp = &source[width];
- BYTE *bp = &source[width*2];
-
- for(i=0;i<width;i++) {
- *(dest++) = *(bp++);
- *(dest++) = *(gp++);
- *(dest++) = *(rp++);
- *(dest++) = 0;
- }
- }
-
- void RGBlines2gray(BYTE *source, BYTE *dest, int width) {
- int j;
- BYTE *rp = source;
- BYTE *gp = &source[width];
- BYTE *bp = &source[width*2];
- for (j = 0; j < width; j++)
- *(dest++) = (*(rp++) * 299L + *(gp++) * 587L + *(bp++) * 114L) / 1000;
- }
-
- void palette2gray(BGRpalette source,BGRpalette dest) {
- int j;
- for (j = 0; j < 256; j++)
- dest[j].g = dest[j].b = dest[j].r =
- (source[j].r * 299L+ source[j].g * 587L + source[j].b * 114L + 500) / 1000;
- }
-
- void paletted2gray(BYTE *source, BYTE *dest,int width, BGRpalette graypal) {
- int j;
- for (j = width; j > 0; j--)
- *(dest++) = graypal[*(source++)].r;
- }
-
- void mono2RGB24(BYTE *source,BYTE *dest,int width) {
- int j,i = 0;
- BYTE b;
- BYTE *rp = dest;
- BYTE *gp = &dest[width];
- BYTE *bp = &dest[width*2];
-
- while (TRUE) {
- b = *(source++);
- for (j = 0; j < 8; j++) {
- *(rp++) = *(gp++) = *(bp++) = (b & 0x80) ? 255 : 0;
- b <<= 1;
- if (i++ >= width) return;
- }
- }
- }
-
- getRGBlines getRGBproc(image_type source_type, image_extention_type source_ext) {
- if (source_type != TRUECOLORIMG) return(NULL);
-
- switch(source_ext) {
- case RGB15: return(RGB15_2RGBlines);
- case RGB16: return(RGB16_2RGBlines);
- case BGR24: return(BGR2RGBlines);
- case BGRA32:return(BGRA32_2RGBlines);
- case RGBPLANE24: return(RGBplanes2RGBlines);
- }
- }
-